import axios from 'axios'
import Qs from 'qs'
import {
    // Loading
    Message
} from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
// var instance = axios.create({
//     baseURL: 'http://www.baidu.com',
//     timeout: 5000
// });
// const loading=Loading.service({text:"加载中，请稍后"});
// let baseurl = "http://47.242.138.216/"
let baseurl = "api"
axios.defaults.baseURL = baseurl
axios.defaults.timeout = 5000
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.headers.get['Content-Type'] = 'application/json;charset=UTF-8';
axios.interceptors.request.use(function(config) {
    // 在发送请求之前做些什么
    // const loading = Loading.service({
    // 	text: "加载中，请稍后"
    // });
    // loading;
    if (window.localStorage.getItem('token')) {
        config.headers.token = window.localStorage.getItem('token')
            // config.headers.Authorization ="74935908-7f0f-46b0-99bc-5dac9fd8ad5f"

    }
    //axios拦截 如果头部有token  就携带token传送
    return config;
}, function(error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});
axios.interceptors.response.use(function(response) {
    // 对响应数据做点什么
    // const loading = Loading.service({
    // 	text: "加载中，请稍后"
    // });
    // loading.close()
    return response;
}, function(err) {
    // 对响应错误做点什么
    console.log(err.response)
    if (err && err.response) {
        switch (err.response.status) {
            case 400:
                err.message = "请求错误"
                break;
            case 401:
                err.message = "未授权，请重新登陆"
                break;
            case 403:
                err.message = "拒绝访问"
                break;
            case 404:
                err.message = "请求错误，找不到资源"
                break;
            case 405:
                err.message = "请求方法未允许"
                break;
            case 408:
                err.message = "请求超时"
                break;
            case 500:
                err.message = '服务器端出错'
                break;
            case 501:
                err.message = '网络未实现'
                break;
            case 502:
                err.message = '网络错误'
                break;
            case 503:
                err.message = '服务不可用'
                break;
            case 504:
                err.message = '网络超时'
                break;
            case 505:
                err.message = 'http版本不支持该请求'
                break;
            default:
                err.message = `连接错误${err.response.status}`
        }
    } else {
        err.message = "连接服务器失败"
    }
    Message(err.message);
    return Promise.reject(err);
});
class HttpRequest {

    static post(url, param) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'post',
                url,
                data: Qs.stringify(param)
            }).then(res => {
                resolve(res)
            }).catch(err => {
                reject(err)
                    // err.$Message.success("失败")
            })
        })
    }
    static get(url, param) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'get',
                url,
                params: param,
            }).then(res => {
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    }
    static delete(url, param) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'delete',
                url,
                params: param,
            }).then(res => {
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    }
}
export default HttpRequest